
	$(function(){
		$("#dictionaryTable").datagrid({
			pagination:true,
			idField:"id"
		})
		$("#dictionaryParent").datagrid({
			idField:"id"
		}) 
		
		$("#dictionaryParent").datagrid({
			onClickRow:function (){
				var id = $("#dictionaryParent").datagrid('getSelected');
				var postData = {'parent.id':id.id};
				$("#dictionaryTable").datagrid("reload",postData);	
			}
			
		})
	})
	function setCondition(){
		var postData = {value : $("#value").val()};
		$("#dictionaryTable").datagrid("reload",postData);
	}
	//添加父类
	function addParent(){
		addDialog("添加类型",500,300,"dictionary/dParent","#dictionParent","dictionary/parentAdd","#dictionaryParent");
	}
	//添加子类
	function addSubGrade(){
		addDialog("添加物品",500,300,"dictionary/dSubGrade","#dictionarySubGrade","dictionary/subGradeAdd","#dictionaryTable");
	}
	
	
	//修改父类
	function editParent(){
		var row = $("#dictionaryParent").datagrid("getSelected");
		if(row == null){
			$.messager.alert("提示","请选中要修改的数据行！","warning");
			return;
		}

		//如果选中了多个，只保留row这个
		$("#dictionaryParent").datagrid("clearSelections");
		$("#dictionaryParent").datagrid("selectRecord",row.id);
		
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title : "修改类型",
			iconCls : "icon-edit",
			width:500,
			height:300,
			modal:true,
			href : "dictionary/dParent",
			onClose:function(){$(this).dialog("destroy"); },
			onLoad:function(){
				//发送异步请求，查询数据
				$.post("dictionary/selectById",{id:row.id},function(data){
					$("#dictionParent").form("load",data);
				});
			},
			buttons:[{
				iconCls:"icon-ok",
				text:"确定",
				handler:function(){
					$("#dictionParent").form("submit",{
						url : "dictionary/parentEdit",
						success : function(data){
							d.dialog("close");
							$("#dictionaryParent").datagrid("reload");
						}
					});
				}
			},{
				iconCls:"icon-cancel",
				text:"取消",
				handler:function(){
					d.dialog("close");
				}
			}]
		})
	}
	
	//修改子类
	function editSubGrade(){
		var row = $("#dictionaryTable").datagrid("getSelected");
		if(row == null){
			$.messager.alert("提示","请选中要修改的数据行！","warning");
			return;
		}

		//如果选中了多个，只保留row这个
		$("#dictionaryTable").datagrid("clearSelections");
		$("#dictionaryTable").datagrid("selectRecord",row.id);
		
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title : "修改物品",
			iconCls : "icon-edit",
			width:500,
			height:300,
			modal:true,
			href : "dictionary/dSubGrade",
			onClose:function(){$(this).dialog("destroy"); },
			onLoad:function(){
				//发送异步请求，查询数据
				$.post("dictionary/selectById",{id:row.id},function(data){
					$("#dictionarySubGrade").form("load",data);
					$("#parentid").combobox("select",data.parent.id);
				});
			},
			buttons:[{
				iconCls:"icon-ok",
				text:"确定",
				handler:function(){
					$("#dictionarySubGrade").form("submit",{
						url : "dictionary/dictionaryEdit",
						success : function(data){
							d.dialog("close");
							$("#dictionaryTable").datagrid("reload");
						}
					});
				}
			},{
				iconCls:"icon-cancel",
				text:"取消",
				handler:function(){
					d.dialog("close");
				}
			}]
		})
	}
	
	//删除子类
	function deleteState(){
		deleteConfirm("#dictionaryTable","dictionary/stateEdit");
	}
	
	$(document).click(function(e,data){
		if($(e.target).parents().length == 0){
			document.location.reload(data);
		}

	});